package com.qlm.controller;

import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.weixin.sdk.api.AccessTokenApi;
import com.jfinal.weixin.sdk.api.ApiConfig;
import com.jfinal.weixin.sdk.api.ApiResult;
import com.jfinal.weixin.sdk.api.MenuApi;
import com.jfinal.weixin.sdk.jfinal.ApiController;
import com.jfinal.weixin.sdk.utils.HttpUtils;
import com.qlm.interceptor.AdminOperateInterceptor;
import com.qlm.interceptor.AuthInterceptor;
import com.qlm.service.IWxMenuService;
import com.qlm.service.impl.WxMenuServiceImpl;
import com.qlm.tools.Common;

public class WXMenuController extends ApiController {

	private IWxMenuService wxmenuService = enhance(WxMenuServiceImpl.class);
	@Override
	public ApiConfig getApiConfig() {
		ApiConfig ac = new ApiConfig();
		
		// 配置微信 API 相关常量
		ac.setToken(PropKit.get("token"));
		ac.setAppId(PropKit.get("appId"));
		ac.setAppSecret(PropKit.get("appSecret"));
		
		/**
		 *  是否对消息进行加密，对应于微信平台的消息加解密方式：
		 *  1：true进行加密且必须配置 encodingAesKey
		 *  2：false采用明文模式，同时也支持混合模式
		 */
		ac.setEncryptMessage(PropKit.getBoolean("encryptMessage", false));
		ac.setEncodingAesKey(PropKit.get("encodingAesKey", "setting it in config file"));
		return ac;
	}
	
	/**
	 * 进入编辑页面
	 */
	public void editMenu(){
		renderJsp("/page/wxMenu.jsp");
	}
	
	/**
	 * 保存自定义菜单
	 */
	@Before({AuthInterceptor.class , AdminOperateInterceptor.class})
	public void saveMenu(){
		//TODO
		String menu = getPara("menu");
		boolean flg = wxmenuService.saveMenu(menu);
		Record record ;
		if(flg)
			record  = new Record().set("state", "true");
		else
			record  = new Record().set("state", "false");
		renderJson(record);
	}
	
	/**
	 * 获取公众号菜单
	 */
	public void getWXMenu() {
//		ApiResult apiResult = MenuApi.getMenu();
//		if (apiResult.isSucceed())
//			renderJson("");
//		else
//			renderJson("");
	}
	
	/**
	 * 获取数据库菜单数据
	 */
	@Before({AuthInterceptor.class , AdminOperateInterceptor.class})
	public void getMenu(){
		String menu = wxmenuService.getMenu();
		if(Common.isEmptyString(menu)){
			renderText(menu);
		}else{
			renderText("");
		}
	}
	
	/**
	 * 保存菜单至数据库，在微信端创建菜单
	 */
	@Before({AuthInterceptor.class , AdminOperateInterceptor.class})
	public void createMenu()
	{	//TODO
		String menu = wxmenuService.getMenu();
		Record record ;
		if(Common.isEmptyString(menu)){
			ApiResult apiResult = MenuApi.createMenu(menu);
			if (apiResult.isSucceed())
				record  = new Record().set("state", "true");
			else
				record  = new Record().set("state", "false");			
		}else{
			record  = new Record().set("state", "false");
		}
		renderJson(record);			
	}
	
	/**
	 * 删除菜单
	 */
	@Before({AuthInterceptor.class , AdminOperateInterceptor.class})
	public void delMenu(){
		String url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token="+AccessTokenApi.getAccessTokenStr();
		String jsonResult = HttpUtils.post(url, "");
		ApiResult apiResult =  new ApiResult(jsonResult);
		Record record ;
		if (apiResult.isSucceed())
			record  = new Record().set("state", "true");
		else
			record  = new Record().set("state", "false");
		renderJson(record);
	}

}
